﻿@namespace Aspire.Dashboard.Components

@using Aspire.Dashboard.Model
@using Aspire.Dashboard.Otlp.Model
@using Aspire.Dashboard.Resources

@inject IStringLocalizer<Columns> Loc

<span class="resource-name-container">
    @* Do this to trim significant whitespace so there isn't a space added between icon and text *@
    @{
        <FluentIcon Width="16px"
                    Class="resource-icon"
                    Color="Color.Custom"
                    CustomColor="@ColorGenerator.Instance.GetColorHexByKey(FormatName(Resource))"
                    Value="@ResourceIconHelpers.GetIconForResource(Resource, IconSize.Size16)" />
    }
    @{
        <span class="resource-name-text"><FluentHighlighter HighlightedText="@FilterText" Text="@FormatName(Resource)" /></span>
    }
    @if (Resource.Properties.TryGetValue(KnownProperties.Container.Lifetime, out var value) &&
        value.Value.HasStringValue &&
        StringComparer.Ordinal.Equals(value.Value.StringValue, KnownContainerLifetimes.Persistent))
    {
        <FluentIcon Icon="Icons.Regular.Size16.Pin" Title="@Loc[nameof(Columns.PersistentContainerIconTooltip)]"
            Color="Color.Neutral" Class="persistent-container-icon" />
    }
</span>

@code {
    [Parameter, EditorRequired]
    public required ResourceViewModel Resource { get; set; }

    [Parameter, EditorRequired]
    public required Func<ResourceViewModel, string> FormatName { get; set; }

    [Parameter, EditorRequired]
    public required string FilterText { get; set; }

    private static class KnownContainerLifetimes
    {
        // Matches members from Aspire.Hosting.ApplicationModel.ContainerLifetime
        public const string Session = nameof(Session);
        public const string Persistent = nameof(Persistent);
    }
}
